package user_assets_usdt

import (
	"context"
	"errors"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/logic/withdraw"
	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/common"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUsdtWalletInsertInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 添加
func NewUserUsdtWalletInsertInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUsdtWalletInsertInfoLogic {
	return &UserUsdtWalletInsertInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserUsdtWalletInsertInfoLogic) UserUsdtWalletInsertInfo(req *types.UserUSDTWalletInsertInfoRequest) (resp *types.UserUSDTWalletInsertInfoResponse, err error) {

	if withdraw.IsOpen(l.svcCtx, l.ctx, common.AllowedAddUsdt, "status") == 0 {
		return &types.UserUSDTWalletInsertInfoResponse{
			IsInsert: false,
		}, errors.New("not allowed adding")
	}

	userId := headInfo.GetTokenUid(l.ctx)
	if userId == 0 {
		return &types.UserUSDTWalletInsertInfoResponse{
			IsInsert: false,
		}, errors.New("user is not exist")
	}
	var usdtType string
	if req.UsdtType == 0 {
		usdtType = ""
	} else {
		usdtType = fmt.Sprintf("%d", req.UsdtType)
	}
	_, err = l.svcCtx.AssetsRpc.UserChannel.UserUsdtWalletsInsert(l.ctx, &rpc.UserUsdtWalletsInsertRequest{
		UserId:       userId,
		Address:      req.Address,
		UsdtType:     usdtType,
		AddressAlias: req.AddressAlias,
	})

	if err != nil {
		return &types.UserUSDTWalletInsertInfoResponse{
			IsInsert: false,
		}, err
	}
	return &types.UserUSDTWalletInsertInfoResponse{
		IsInsert: true,
	}, err
}
